package com.efeichong.project.service.impl;

import com.efeichong.common.exception.CustomException;
import com.efeichong.common.utils.OptionEmptyFunction;
import com.efeichong.jpa.JExample;
import com.efeichong.project.domain.dao.db.SysMenuDao;
import com.efeichong.project.domain.entity.db.SysMenu;
import com.efeichong.project.domain.vo.SysMenuVo;
import com.efeichong.project.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author
 * @date 2020/11/25
 * @description 权限
 */
@RequiredArgsConstructor
@Service
public class SysMenuServiceImpl implements SysMenuService {

    private final SysMenuDao sysMenuDao;

    /**
     * 通过id查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public SysMenuVo getInfo(Long id) {
        return sysMenuDao.selectById(id, SysMenuVo.class).orElseThrow(OptionEmptyFunction.create());
    }

    /**
     * 权限列表查询
     *
     * @param sysMenuVo
     * @return
     */
    @Override
    public List<SysMenuVo> selectByList(SysMenuVo sysMenuVo) {
        JExample<SysMenu> example = new JExample();
        example.orderBy().asc("orderNum");
        example.groupBy("id");
        example.initExample(sysMenuVo);
        return sysMenuDao.selectAll(example, SysMenuVo.class);
    }

    /**
     * 新增
     *
     * @param sysMenuVo
     * @return
     */
    @Transactional
    @Override
    public void insertSysMenu(SysMenuVo sysMenuVo) {
        SysMenu sysMenu = sysMenuVo.toPo(SysMenu.class);
        sysMenu.setLeaves(1);
        if (sysMenuVo.getParentId() != null) {
            SysMenu parent = sysMenu.getParent();
            parent.setLeaves(0);
            sysMenuDao.save(parent);
        }
        sysMenuDao.save(sysMenu);
    }

    /**
     * 修改
     *
     * @param sysMenuVo
     * @return
     */
    @Transactional
    @Override
    public void updateSysMenu(SysMenuVo sysMenuVo) {
        if (sysMenuVo.getId() == null) {
            throw new CustomException("主键为必传参数");
        }
        SysMenu sysMenu = sysMenuVo.toPo(SysMenu.class);
        sysMenu = sysMenuDao.save(sysMenu);
        if (sysMenuVo.getParentId() != null) {
            SysMenu parent = sysMenu.getParent();
            if (parent.getLeaves() == 1) {
                parent.setLeaves(0);
                sysMenuDao.save(parent);
            }
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void deleteById(Long id) {
        Optional<SysMenu> optional = sysMenuDao.selectById(id);
        if (optional.isPresent()) {
            SysMenu sysMenu = optional.get();
            sysMenuDao.deleteById(id);
            SysMenu parent = sysMenu.getParent();
            if (parent != null && CollectionUtils.isNotEmpty(parent.getChildren())) {
                parent.getChildren().remove(sysMenu);
            }
            if (parent != null && CollectionUtils.isEmpty(parent.getChildren())) {
                parent.setLeaves(1);
                sysMenuDao.save(parent);
            }
        } else {
            throw new CustomException("数据不存在！");
        }

    }
}
